package com.lys.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

public class XmlBuilder {
    private final static Logger logger= LoggerFactory.getLogger(XmlBuilder.class);
    public static Object xmlToObject(Class<?> clazz,String xmlStr){
        Object obj=null;
        JAXBContext context=null;
        Reader reader=null;
        reader=new StringReader(xmlStr);
        try {
            context=JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            obj= unmarshaller.unmarshal(reader);
        } catch (JAXBException e) {
            logger.error("JAXBException",e);

        }
        if (reader!=null){
            try {
                reader.close();
            } catch (IOException e) {
                logger.error("IOException",e);
            }
        }
        return obj;

    }
}
